package com.ruoyi.common.config;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.YamlUtil;
import com.ruoyi.common.utils.spring.SpringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;

/**
 * 全局配置类
 *
 * @author mars
 */
public class Global {
    private static final Logger log = LoggerFactory.getLogger(Global.class);

    private static String NAME;
    private static String DEFAULT_NAME = "application.yml";

    //修改全局配置文件加载
    static {
        RuntimeEnv bean = SpringUtils.getBean(RuntimeEnv.class);
        String env = bean.getProfile();
        if (StringUtils.isBlank(env)) {
            NAME = "application.yml";
        } else {
            NAME = String.format("application%s.yml", "-" + bean.getProfile());
        }

    }

    /**
     * 当前对象实例
     */
    private static Global global;

    /**
     * 保存全局属性值
     */
    private static Map<String, String> map = new HashMap<String, String>();

    private Global() {
    }

    /**
     * 静态工厂方法
     */
    public static synchronized Global getInstance() {
        if (global == null) {
            global = new Global();
        }
        return global;
    }

    /**
     * 获取配置
     */
    public static String getConfig(String key) {
        String value = map.get(key);
        if (value == null) {
            Map<?, ?> yamlMap = null;
            try {
                yamlMap = YamlUtil.loadYaml(NAME);
                Object property = YamlUtil.getProperty(yamlMap, key);
                //读取默认配置
                if (property == null) {
                    yamlMap = YamlUtil.loadYaml(DEFAULT_NAME);
                    property = YamlUtil.getProperty(yamlMap, key);
                }
                map.put(key, property != null ? String.valueOf(property) : StringUtils.EMPTY);
            } catch (FileNotFoundException e) {
                log.error("获取全局配置异常 {}", key);
            }
        }
        return value;
    }

    /**
     * 获取项目名称
     */
    public static String getName() {
        return StringUtils.nvl(getConfig("mars.name"), "mars");
    }

    /**
     * 获取项目版本
     */
    public static String getVersion() {
        return StringUtils.nvl(getConfig("mars.version"), "1.0.0");
    }

    /**
     * 获取版权年份
     */
    public static String getCopyrightYear() {
        return StringUtils.nvl(getConfig("mars.copyrightYear"), "2019");
    }

    /**
     * 实例演示开关
     */
    public static String isDemoEnabled() {
        return StringUtils.nvl(getConfig("mars.demoEnabled"), "true");
    }

    /**
     * 获取ip地址开关
     */
    public static Boolean isAddressEnabled() {
        return Boolean.valueOf(getConfig("mars.addressEnabled"));
    }

    /**
     * 获取文件上传路径
     */
    public static String getProfile() {
        return getConfig("mars.profile");
    }

    /**
     * 获取头像上传路径
     */
    public static String getAvatarPath() {
        return getProfile() + "/avatar";
    }

    /**
     * 获取下载路径
     */
    public static String getDownloadPath() {
        return getProfile() + "/download/";
    }

    /**
     * 获取上传路径
     */
    public static String getUploadPath() {
        return getProfile() + "/upload";
    }


    /***********************阿里云上传配置*************************/

    /**
     * 获取oss 的 endpoint
     *
     * @return
     */
    public static String getOssEndPoint() {
        return StringUtils.nvl(getConfig("aliyun.oss.endpoint"), "https://oss-cn-shenzhen.aliyuncs.com");
    }

    /**
     * 获取oss 的 accessKeyId
     *
     * @return
     */
    public static String getAliyunAccessKeyId() {
        return StringUtils.nvl(getConfig("aliyun.accessKeyId"), "LTAISHGFy52TOGhc");
    }

    /**
     * 获取oss 的 accessKeySecret
     *
     * @return
     */
    public static String getAliyunAccessKeySecret() {
        return StringUtils.nvl(getConfig("aliyun.accessKeySecret"), "zfimu8fYCMxB2iRPQYM7x9Ez26gC4Y");
    }

    /**
     * 获取oss 的 bucketName
     *
     * @return
     */
    public static String getOssBucketName() {
        return StringUtils.nvl(getConfig("aliyun.oss.bucketName"), "bius");
    }
}